import WXP from './minapp-request/wxp.min.js'
import requestIntercept from './minapp-request/requestIntercept.js'
import util from './util_method.js'
const apiurl = 'https://djapp.ycf.test.tsingc.com/api/';
const host = 'https://djapp.ycf.test.tsingc.com';
// 注册请求拦截器
WXP.intercept('request', requestIntercept);
const $ajax = (
    {    
        url = '',
        data = {},
        methods = 'POST',
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        loading = '加载中...'
    },
    {
        success = function (res) {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        },
        fail = function (res) {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        },
      complete = function (res) {
        // 控制台调试日志
        // 隐藏加载提示
        wx.hideNavigationBarLoading();
        // 停止下拉状态
        wx.stopPullDownRefresh();
      }
    } = {}, //默认为空对象
) => {
    // 增强体验：加载中
    wx.showNavigationBarLoading();
    if (loading && loading !== false) {
        WXP.showLoading({
            title: loading,
            icon: 'none'
        })
    }
   
    // 构造请求体
    let request = {
        url: url.indexOf(apiurl) > -1 ? url : apiurl + url,
        method: methods,
        header: Object.assign({
            // set something global
        }, headers),
        data: Object.assign({
            
        }, data)
    }
     
    WXP.request(Object.assign(request)).then(resp => {
      //大于0代表成功  200
      if (resp.data.code > 0) {
        util.isFunction(success) && success(resp.data);
      } else {
        util.isFunction(fail) && fail(resp.data);
      }
      util.isFunction(complete) && complete(resp.data)
    }).catch(errorMesg => {
      console.log(errorMesg);
      // 隐藏加载提示 
      if (util.isFunction(complete)) {
        complete(errorMesg)
      }
      wx.hideNavigationBarLoading();
    })
}

const switchTab = function(path, cb) {
  wx.switchTab({
    url: path,
    success: function() {
      typeof cb == "function" && cb();
    },
    fail: function() {
      console.log('跳转失败');
    }
  })
}
const complete = function (that, res, app, page = 15) {
  let tip = '';
  if (app.util.isString(res)) {
    app.info(res);
    tip = '上拉加载';
    that.data.is_pull = true;
  } else if (!app.util.isUndefined(res.data) && app.util.isArray(res.data.data)) {
    if (res.data.data.length < page) {
      that.data.is_pull = false;
      tip = '没有更多数据'
    } else {
      tip = '上拉加载';
      that.data.is_pull = true;
    }
  }
  that.setData({
    loading: false,
    tip: tip,
  })
}
module.exports = { 
    $ajax: $ajax,
    apiurl: apiurl,
    host: host,
    WXP: WXP, 
    switchTab,
    complete
}

